/*
-----------------------------------------------------------------------------
This source file is part of the OGRE Reference Application, a layer built
on top of OGRE(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/

Copyright (c) 2000-2006 Torus Knot Software Ltd
Also see acknowledgements in Readme.html

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt.

You may alternatively use this source under the terms of a specific version of
the OGRE Unrestricted License provided you have obtained such a license from
Torus Knot Software Ltd.
-----------------------------------------------------------------------------
*/
#include "OgreRefAppBall.h"
#include "OgreRefAppApplicationObject.h"
#include "OgreRefAppPlane.h"
#include "OgreRefAppSoccerTeam.h"
#include "OgreRefAppGoal.h"
#include "OgreRefAppSoccerPitch.h"
#include "OgreRefAppWorld.h"

#define __DEBUGON__

const int NumRegionsHorizontal = 6;
const int NumRegionsVertical   = 3;



namespace OgreRefApp
{

    SoccerPitch::SoccerPitch(int cx, int cy):m_cxClient(cx),
            m_cyClient(cy),
            m_bPaused(false),
            m_bGoalKeeperHasBall(false),
            m_Regions(NumRegionsHorizontal*NumRegionsVertical),
            m_bGameOn(true)
    {
        //define the playing area
        m_pPlayingArea = new Region(FieldWidth, FieldHeight, 0, 0);

        //create the regions
        createRegions(TileWidth,
                TileHeight);



        //create the goals
        m_pBlueGoal  = new Goal(Ogre::Vector3( m_pPlayingArea->Left(), (FieldHeight-GoalWidth)/2,BallRadius),
              Ogre::Vector3(m_pPlayingArea->Left(), (FieldHeight+GoalWidth)/2,BallRadius),
              Ogre::Vector3(1,0,BallRadius));



        m_pRedGoal = new Goal( Ogre::Vector3( m_pPlayingArea->Right(), (FieldHeight-GoalWidth)/2,BallRadius),
              Ogre::Vector3(m_pPlayingArea->Right(), (FieldHeight+GoalWidth)/2,BallRadius),
              Ogre::Vector3(-1,0,BallRadius));


        //create the soccer ball

        m_pBall = World::getSingletonPtr()->createBall("ball", 7 ,
                  Vector3(400,
                          200,
                          10
                         ));
        m_pBall->setDynamicsEnabled(true, true);
        m_pBall->getEntity()->setMaterialName("Examples/Hilite/White");


                World::getSingletonPtr()->createBox("wall-top", 800, 1, 800, Vector3(400, 0, 0));
        World::getSingletonPtr()->createBox("wall-right", 1, 400, 800, Vector3(0, 200, 0));
        World::getSingletonPtr()->createBox("wall-down", 800, 1, 800, Vector3(400, 400, 0));
        World::getSingletonPtr()->createBox("wall-left", 1, 400, 800, Vector3(800, 200, 0));

        m_pPlane = World::getSingletonPtr()->createPlane("field",400,200);
        m_pPlane->setPosition(400,200,0);
        m_pPlane->getEntity()->setMaterialName("Examples/GrassFloor");

        //create the teams
        m_pRedTeam  = new SoccerTeam(m_pRedGoal, m_pBlueGoal, this, SoccerTeam::red);
        m_pBlueTeam = new SoccerTeam(m_pBlueGoal, m_pRedGoal, this, SoccerTeam::blue);
        m_pRedTeam->SetOpponents(m_pBlueTeam);
        m_pBlueTeam->SetOpponents(m_pRedTeam);



    }


    //------------------------- CreateRegions --------------------------------
    void SoccerPitch::createRegions(Real width, Real height)
    {
      //index into the vector
      int idx = m_Regions.size()-1;

      for (int col=0; col<NumRegionsHorizontal; ++col)
      {
        for (int row=0; row<NumRegionsVertical; ++row)
        {

          m_Regions[idx--] = new Region(Ogre::Vector2(col, row),
                                       idx);
        }
      }
    }


//-------------------------------- dtor ----------------------------------
//------------------------------------------------------------------------
    SoccerPitch::~SoccerPitch()
    {
        delete m_pBall;

        delete m_pRedTeam;
        delete m_pBlueTeam;

        delete m_pRedGoal;
        delete m_pBlueGoal;

        delete m_pPlayingArea;

        delete m_pPlane;



        for (unsigned int i=0; i<m_Regions.size(); ++i)
        {
            delete m_Regions[i];
        }
    }

//----------------------------- Update -----------------------------------
//
//  this demo works on a fixed frame rate (60 by default) so we don't need
//  to pass a time_elapsed as a parameter to the game entities
//------------------------------------------------------------------------
    void SoccerPitch::update()
    {
        /*
        #ifdef __DEBUGON__
        std::cout << std::endl << "SoccerPitch::update start" << std::endl << std::endl;
        #endif
        */
        //update the balls
        //m_pBall->update();

        //update the teams
        m_pRedTeam->update();
        m_pBlueTeam->update();
        if( !m_bGameOn ) {
            m_pBall->setPosition(400, 200, 15);
            m_pBall->setLinearVelocity(0,0,0);
            m_pBall->setAngularVelocity(0,0,0);

        }

        //if a goal has been detected reset the pitch ready for kickoff
        if (( fabs(m_pBlueGoal->Center().y - m_pBall->getPosition().y) <= GoalWidth/2
                && ( fabs(m_pBlueGoal->Center().x - m_pBall->getPosition().x ) <= 20 )
                && ( fabs(m_pBlueGoal->Center().z - m_pBall->getPosition().z ) <= 50 ) )
                || (( fabs(m_pBlueGoal->Center().y - m_pBall->getPosition().y) <= GoalWidth/2 )
                 && ( fabs(m_pRedGoal->Center().x - m_pBall->getPosition().x) <= 20 )
                 && ( fabs(m_pBlueGoal->Center().z - m_pBall->getPosition().z ) <= 50 )))
        {
        m_bGameOn=false;
        //reset the ball
        m_pBall->setPosition(400, 200, 15);
        m_pBall->setLinearVelocity(0,0,0);
        m_pBall->setAngularVelocity(0,0,0);


        //get the teams ready for kickoff
        m_pRedTeam->GetFSM()->ChangeState(PrepareForKickOff::Instance());
        m_pBlueTeam->GetFSM()->ChangeState(PrepareForKickOff::Instance());
        }



    }





}
